package com.ls.atf.core.web.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.ls.atf.core.web.WebPage;
import com.ls.atf.core.web.elements.Button;
import com.ls.atf.core.web.elements.TextInput;
import com.ls.atf.data.UserData;

public class MailLoginPage extends WebPage<MailLoginPage> {
	
	private static final String PAGE_URL = "https://mail.google.com";

	private static final String EMAIL_INPUT_ID = "Email";
	private static final String PASS_INPUT_ID = "Passwd";
	private static final String LOGIN_BUTTON_ID = "signIn";
	
	private String email;

	public MailLoginPage(WebDriver driver, String email) {
		super(driver);
		this.email = email;
	}

	@Override
	public MailLoginPage load() {
		driver.get(PAGE_URL);
		return this;
	}

	@Override
	public boolean isAvailable() {
		return getEmailInput().isAvailable() &&
				getPasswordInput().isAvailable() &&
				getLoginButton().isAvailable();
	}
	
	public MailInboxPage loginAs(UserData user) {
		getEmailInput().inputText(user.getEmail());
		getPasswordInput().inputText(user.getPassword());
		getLoginButton().click();
		return new MailInboxPage(driver, user, email).waitUntilAvailable();
	}

	private TextInput getEmailInput() {
		return new TextInput(driver, By.id(EMAIL_INPUT_ID));
	}

	private TextInput getPasswordInput() {
		return new TextInput(driver, By.id(PASS_INPUT_ID));
	}
	
	private Button getLoginButton() {
		return new Button(driver, By.id(LOGIN_BUTTON_ID));
	}

}
